//package com.my.getway.white;
//
//import com.alibaba.fastjson.JSONObject;
//import lombok.extern.slf4j.Slf4j;
//import org.jeecg.common.CommonConstant;
//import org.jeecg.rservice.RBaseProjectService;
//import org.jeecg.rservice.vo.Result;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.util.Arrays;
//import java.util.stream.Collectors;
//
//import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
//import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.addOriginalRequestUrl;
//
///**
// *
// */
//@Slf4j
//@Component
//public class GlobalAccessTokenFilter implements GlobalFilter, Ordered, InitializingBean {
//
//
//
//
//    @Autowired
//    private WhitePageListConfig whitePageListConfig;
//
//    @Autowired
//    private RBaseProjectService rBaseProjectService;
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        /**
//        String url = exchange.getRequest().getURI().getPath();
//
//        log.info("  access url :  "+ url);
//
//        // 1. 重写StripPrefix(获取真实的URL)
//        addOriginalRequestUrl(exchange, exchange.getRequest().getURI());
//        String rawPath = exchange.getRequest().getURI().getRawPath();
//        String newPath = "/" + Arrays.stream(StringUtils.tokenizeToStringArray(rawPath, "/")).skip(1L).collect(Collectors.joining("/"));
//        ServerHttpRequest newRequest = exchange.getRequest().mutate().path(newPath).build();
//        exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());
//
//        //将现在的request，添加当前身份
//        ServerHttpRequest mutableReq = exchange.getRequest().mutate().header("Authorization-UserName", "").build();
//        ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
//        return chain.filter(mutableExchange);
//         **/
//        String url = exchange.getRequest().getURI().getPath();
//
//        log.info("  access url :  "+ url);
//
//        // 1. 重写StripPrefix(获取真实的URL)
//        addOriginalRequestUrl(exchange, exchange.getRequest().getURI());
//        String rawPath = exchange.getRequest().getURI().getRawPath();
//        String newPath = "/" + Arrays.stream(StringUtils.tokenizeToStringArray(rawPath, "/")).skip(1L).collect(Collectors.joining("/"));
//        ServerHttpRequest newRequest = exchange.getRequest().mutate().path(newPath).build();
//        exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());
//
//        //将现在的request，添加当前身份
//        ServerHttpRequest mutableReq = exchange.getRequest().mutate().header("Authorization-UserName", "").build();
//        ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
//
//        if (url.contains(ServiceNameConstants.VIEW_BOARD_SERVICE) || url.contains(ServiceNameConstants.WANGGE_EXAM_SERVICE)){
//            //API 白名单
//            if (whitePageListConfig.isAtWhitePageList (url)){
//                passed(exchange,chain,null);
//            }
//            //获取header
//            String token  = exchange.getRequest().getHeaders().getFirst(CommonConstant.X_ACCESS_TOKEN);
//            Result<?> result =   rBaseProjectService.parseToken(token);
//            if (null != result){
//                if (result.getCode() == 200){
//                    Object obj = result.getResult();
//                    if (null != obj){
//                        //JSONObject.pars
//                        return passed(exchange,chain,obj);
//                    }
//                }
//            }
//            return fail(exchange);
//
//        }
//
//
//        return chain.filter(mutableExchange);
//
//
//
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//
//
//    //对 view board 模块的处理
//    private Mono<Void> passed(ServerWebExchange exchange, GatewayFilterChain chain, Object data){
//        JSONObject jo= new JSONObject();
//        if (null != data){
//            jo = (JSONObject) JSONObject.toJSON(data);
//
//        }
//        ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CommonConstant.X_ACCESS_USERNAME, jo.getString("username"))
//                .header(CommonConstant.X_ACCESS_ID,jo.getString("id"))
//                .build();
//        ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
//        return chain.filter(mutableExchange);
//    }
//
//    private Mono<Void> fail(ServerWebExchange exchange){
//        ServerHttpResponse response = exchange.getResponse();
//        response.setStatusCode(HttpStatus.UNAUTHORIZED);
//       // response.writeAndFlushWith()
//        return response.setComplete();
//    }
//
//
//
//
//
//
//
//
//
//    @Override
//    public void afterPropertiesSet() throws Exception {
//        whitePageListConfig.init();
//    }
//}
